#pragma once

#include "Common.hpp"
#include "Protocol.hpp"

class Calculate
{
public:
    Calculate(){}

    // 进行计算：来一个请求结构体，返回一个答案结构体
    Response Execute(const Request& req)
    {
        int x = req.X(), y = req.Y();
        char operation = req.Oper();
        Response resp;
        unordered_map<char, function<int(int,int)>> mp = {
            { '+', [](int x, int y)->int{ return x + y; } }, 
            { '-', [](int x, int y)->int{ return x - y; } },
            { '*', [](int x, int y)->int{ return x * y; } },
            { '/', [](int x, int y)->int{ if(y == 0) return -1;return x / y; } },
            { '%', [](int x, int y)->int{ if(y == 0) return -1;return x % y; } },
        };
        if(!mp.contains(operation)) resp.SetCode(2);  // 错误码2表示没有这个操作符
        else
        {
            int ret = mp[operation](x, y);
            if(ret == -1) resp.SetCode(1);  // 错误码1就是除0错误
            else resp.SetResult(ret);
        }
        return resp; 
    }

    ~Calculate(){}
private:
};